<template>
  <transition name="message-fade">
    <div v-if="visible" :class="['custom-message', `custom-message--${type}`]">
      <div class="message-content">
        <el-icon class="message-icon">
          <SuccessFilled v-if="type === 'success'" />
          <WarningFilled v-if="type === 'warning'" />
          <CircleCloseFilled v-if="type === 'error'" />
          <InfoFilled v-if="type === 'info'" />
        </el-icon>
        <span class="message-text">{{ message }}</span>
      </div>
      <el-icon class="message-close" @click="close">
        <Close />
      </el-icon>
    </div>
  </transition>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { SuccessFilled, WarningFilled, CircleCloseFilled, InfoFilled, Close } from '@element-plus/icons-vue'

const props = defineProps({
  message: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'info',
    validator: (value) => ['success', 'warning', 'error', 'info'].includes(value)
  },
  duration: {
    type: Number,
    default: 3000
  }
})

const emit = defineEmits(['close'])

const visible = ref(false)

const close = () => {
  visible.value = false
  setTimeout(() => {
    emit('close')
  }, 300)
}

onMounted(() => {
  visible.value = true
  if (props.duration > 0) {
    setTimeout(() => {
      close()
    }, props.duration)
  }
})
</script>

<style scoped>
.custom-message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  border: 1px solid #e1e5e9;
  padding: 16px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 300px;
  max-width: 500px;
  backdrop-filter: blur(10px);
}

.message-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.message-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.message-text {
  font-size: 14px;
  font-weight: 500;
  color: #1f2937;
  line-height: 1.4;
}

.message-close {
  font-size: 16px;
  color: #6b7280;
  cursor: pointer;
  transition: color 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  margin-left: 12px;
  flex-shrink: 0;
}

.message-close:hover {
  color: #374151;
}

/* 不同类型的样式 */
.custom-message--success {
  border-left: 4px solid #10b981;
}

.custom-message--success .message-icon {
  color: #10b981;
}

.custom-message--warning {
  border-left: 4px solid #f59e0b;
}

.custom-message--warning .message-icon {
  color: #f59e0b;
}

.custom-message--error {
  border-left: 4px solid #ef4444;
}

.custom-message--error .message-icon {
  color: #ef4444;
}

.custom-message--info {
  border-left: 4px solid #3b82f6;
}

.custom-message--info .message-icon {
  color: #3b82f6;
}

/* 动画效果 */
.message-fade-enter-active,
.message-fade-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.message-fade-enter-from {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}

.message-fade-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}
</style>